(function($) {

	$.fn.isSquare = function() {

		return $(this).each(function() {

			var nextGalleryIndex = 1,
				duobeiImagePath = $('#duobei_images_path').val(),
				isFirstTimeToSquare = $('#is_first_time_to_square').val(),
				isSnsNewAccount = $('#is_sns_new_account').val(),
				isAnonymous = $('#is_anonymous').val(),
				galleryHoverTimeOut;

			//扩展动画效果
			$.extend($.easing,{easeOutSine:function(x,t,b,c,d){return c*Math.sin(t/d*(Math.PI/2))+b;}});

			//大图轮转的滑动效果
			$('#gallery').slides({
				pause		: 1,
				slideSpeed	: 350,
				play		: 5000,
				crossfade	: true,
				hoverPause	: true,
				preload		: true,
				slideEasing	: 'easeOutSine',
				preloadImage: [duobeiImagePath, '/uploading-32-32-20111227.gif'].join(''),
				animationStart: function() {
					$('#display_gallery_time').fadeOut('100');
					$('.preview_item').removeClass('current');
					$('.preview_item:eq(' + nextGalleryIndex + ')').addClass('current');
				},
				animationComplete: function(current) {
					$('#display_gallery_time').text($('.gallery_display:eq(' + (current - 1) + ')>.course_time').text()).fadeIn('1000');
					$('.preview_item').removeClass('current');
					$('.preview_item:eq(' + (current - 1) + ')').addClass('current');
					
					nextGalleryIndex = current === 3 ? 0 : current;
				}
			});

			//鼠标浮到大图的缩略图上时，滑动到相应图片
			$('.preview_item').hover(function() {
				if($('#gallery .slides_control').is(':animated')) {
					$('#gallery .slides_control').stop(false, true);
				}
				if($('#display_gallery_time').is(':animated')) {
					$('#display_gallery_time').stop(true, true).show();
				}
				var galleryPageNo = $('.preview_item').index(this);
				nextGalleryIndex = galleryPageNo;
				$('li:eq(' + galleryPageNo + ') > a', $('#gallery')).click();
			});

			$('input').placeholder();

			$('#search_form').submit(function() {
				var searchInput = $(this).find('input[name="word"]');

		    	if($.trim(searchInput.val()) == '') {
					searchInput.css('background', '#f5cfcf');
					searchInput.animate({
						opacity: 0.3
					}, 300, function() {
						searchInput.css('background','#ffffff').css('opacity', '1');
					});
					return false;
				}
				return true;
			});

			$('.course_img', $('#playback_list')).hover(function() {
				$(this).css('opacity', '0.8');
				$('.play_icon', $(this).parent()).css('opacity', '1');
			}, function() {
				$(this).css('opacity', '1');
				$('.play_icon', $(this).parent()).css('opacity', '0.8');
			});

		    $('.play_icon', $('#playback_list')).mouseenter(function() {
				$(this).css('opacity', '1');
				$('.course_img', $(this).parent()).css('opacity', '0.8');
			});

		    if(isFirstTimeToSquare === 'false') {
				$.wokaobox.open({
					'href': [duobeiImagePath, '/discover-first-step-560-285-20111231.png'].join('')
				});
			}

		    if(isSnsNewAccount === 'true') {
				$.wokaobox.open({
					'width'			: 552,
					'height'		: 330,
					'type'			: 'iframe',
					'href'			: [$.global.getContextPath(), '/account/complete'].join(''),
					'afterClosed'	: function() {
						location.href = $.global.getContextPath() + '/square';
					}
				});
			}

			if(isAnonymous) {
				//弹出底部信息
				setTimeout(function(){
					$('.duobei_intro_bar', $('#square_index')).slideDown({
						duration: 1000,
						easing: 'easeOutSine'
					});
				}, 200);

				//隐藏底部信息
				$('.hide_intro_btn').click(function(){
					$.ajax({
						url: [$.global.getContextPath(), '/cancel/fastlogin/pop'].join(''),
						type: 'get',
						complete: function() {
							$('.duobei_intro_bar', $('#square_index')).slideUp('slow');
						}
					});
				});

				//观看视频
				$('a.play_btn, a.screen_capture').hover(function() {
					$('a.play_btn').css('opacity', '1');
				}, function() {
					$('a.play_btn').css('opacity', '0.7');
				}).wokaobox({
					type: 'iframe',
					href: [$.global.getContextPath(), '/video/recognition'].join(''),
					width: 640,
					height: 443,
					helpers: {overlay:{css:{cursor:'default'},closeClick:false}}
				});

				//登录
				$('.login_duobei').click(function(){
					$('#anonymous-login').click();
					return false;
				});
			}
		});
	};
})(jQuery);